/**
 * 
 */
package com.easidea.tim.action.util;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import jxl.CellView;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment;
import jxl.write.DateFormat;
import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.NumberFormat;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.log4j.Logger;

import com.easidea.tim.action.base.BaseAction;
import com.easidea.tim.dto.report.FinanceAccountAgeReportDTO;
import com.easidea.tim.entity.customer.Customer;
import com.easidea.tim.entity.finance.AccountAgeReport;
import com.easidea.tim.entity.finance.CustomerAccountListing;
import com.easidea.tim.service.commodity.ProductService;
import com.easidea.tim.service.customer.CustomerService;
import com.easidea.tim.service.finance.AccountAgeReportService;
import com.easidea.tim.service.finance.CustomerAccountListingService;
import com.opensymphony.webwork.ServletActionContext;

/**
 * @author Harrison
 * 
 */
public class FinanceDownloadAction extends BaseAction {

	private static final Logger log = Logger
			.getLogger(FinanceDownloadAction.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = 8666201609391861929L;

	private String filename;
	private CustomerService customerService;
	private CustomerAccountListingService customerAccountListingService;
	private ProductService productService; 
	private AccountAgeReportService accountAgeReportService;

	private String customerName;
	private Long customerId;
	private Integer year;
	private Integer month;
	private String startDate;
	private String endDate;
	
	private String productName;
	private String colorCode;
	
	/**
	 * 下载客户对账信息清单
	 * 
	 * @return
	 */
	public String downloadCAChecingArray() {
		if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)
				&& customerAccountListingService != null) {
			try {
				Date stDate = parseDate(startDate,null);
				Date edDate = parseDate(endDate, null);
				List<CustomerAccountListing> list = customerAccountListingService
						.getCustomerAccountListingArray(customerId, stDate, getNextDate(edDate));
				filename = URLEncoder.encode("客户对账单-" + customerName + "-" + getDateStr()
						+ ".xls", "UTF-8");

				HttpServletResponse response = ServletActionContext
						.getResponse();
				response
						.setContentType("application/octet-stream;charset=utf-8");
				response.setCharacterEncoding("utf-8");
				response.setHeader("Content-Disposition",
						"attachment;filename=\"" + filename + "\"");
				response.setBufferSize(4096);
				OutputStream outStream = response.getOutputStream();
				excelStreamOutput(list, 3, outStream);
			} catch (Exception e) {
				log.error("out put excel error : ", e);
			}
		}
		return null;
	}
	
	
	/**
	 * 下载客户收支记录清单
	 * 
	 * @return
	 */
	public String downloadCustomerAccountListingArray() {
		if (year != null && month != null
				&& customerAccountListingService != null) {
			try {
				List<CustomerAccountListing> list = customerAccountListingService
					.getCustomerAccountListingArray(customerName, year
								.intValue(), month.intValue());
				filename = URLEncoder.encode("客户收支记录清单-" + getDateStr()
						+ ".xls", "UTF-8");

				HttpServletResponse response = ServletActionContext
						.getResponse();
				response
						.setContentType("application/octet-stream;charset=utf-8");
				response.setCharacterEncoding("utf-8");
				response.setHeader("Content-Disposition",
						"attachment;filename=\"" + filename + "\"");
				response.setBufferSize(4096);
				OutputStream outStream = response.getOutputStream();
				excelStreamOutput(list, 2, outStream);
			} catch (Exception e) {
				log.error("out put excel error : ", e);
			}
		}
		return null;
	}

	/**
	 * 下载客户应收款清单
	 * 
	 * @return
	 * @throws Exception
	 */
	public String downloadCustomerDueFromList() {
		try {
			if (customerService != null) {
				List<Customer> custList = customerService.getAll();
				filename = URLEncoder.encode(
						"客户应收款清单-" + getDateStr() + ".xls", "UTF-8");

				HttpServletResponse response = ServletActionContext
						.getResponse();
				response
						.setContentType("application/octet-stream;charset=utf-8");
				response.setCharacterEncoding("utf-8");
				response.setHeader("Content-Disposition",
						"attachment;filename=\"" + filename + "\"");
				response.setBufferSize(4096);
				OutputStream outStream = response.getOutputStream();
				excelStreamOutput(custList, 1, outStream);
			}
		} catch (Exception e) {
			log.error("output excel error : ", e);
		}
		return null;
	}

	/**
	 * 下载账龄报表
	 * 
	 * @return
	 */
	public String getAccountAgeReport() {
		try {
			if (accountAgeReportService != null) {
				List<AccountAgeReport> list = accountAgeReportService.queryList(null,null); // 提取账龄报表
//				List<FinanceAccountAgeReportDTO> list = customerAccountListingService
//						.accountAgeReport();
				filename = URLEncoder.encode("客户账龄报表-" + getDateStr() + ".xls",
						"UTF-8");

				HttpServletResponse response = ServletActionContext
						.getResponse();
				response
						.setContentType("application/octet-stream;charset=utf-8");
				response.setCharacterEncoding("utf-8");
				response.setHeader("Content-Disposition",
						"attachment;filename=\"" + filename + "\"");
				response.setBufferSize(4096);
				OutputStream outStream = response.getOutputStream();
				excelAccountAgeReport(list, outStream);
			}
		} catch (Exception e) {
			log.error("output excel error : ", e);
		}
		return null;
	}

	/**
	 * 下载价格产品清单
	 * 
	 * @return
	 */
	public String downloadPriceProductList(){
		List<Object[]> pdtList = productService.getPricedProductList(productName, colorCode);
		
		try {
			filename = URLEncoder.encode(
					"产品价格清单.xls", "UTF-8");

			HttpServletResponse response = ServletActionContext
					.getResponse();
			response
					.setContentType("application/octet-stream;charset=utf-8");
			response.setCharacterEncoding("utf-8");
			response.setHeader("Content-Disposition",
					"attachment;filename=\"" + filename + "\"");
			response.setBufferSize(4096);
			OutputStream outStream = response.getOutputStream();
			excelStreamOutput(pdtList, 4, outStream);
			
		} catch (Exception e) {
			log.error("output excel error : ", e);
		}
		return null;
	}
	
	/**
	 * 获取日期格式
	 * 
	 * @return
	 */
	private String getDateStr() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(new Date());
	}
	
	private Date getNextDate(Date date){
		Calendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH,1);
		return cal.getTime();
	}

	/**
	 * 生成excel数据流
	 * 
	 * @param list
	 */
	private void excelStreamOutput(List<?> list, int type, OutputStream out)
			throws IOException, WriteException {
		if (list != null && list.size() > 0 && out != null) {
			switch (type) {
			case 1:
				// customerDueFromList excel output
				genCustomerDueFromListExcel((List<Customer>) list, out);
				break;

			case 2:
				// customerAccountListing excel output
				genCustomerAccountListExcel(
						(List<CustomerAccountListing>) list, out, false);
				break;
				
			case 3:
				// 客户对账清单
				genCustomerAccountListExcel(
						(List<CustomerAccountListing>) list, out, true);
				break;
			case 4:
				// 产品价格清单
				genPriceProductListExcel((List<Object[]>)list,out);
				break;
			}
			
			out.flush();
			out.close();
		}

	}
	
	private void genPriceProductListExcel(List<Object[]> pdtList, OutputStream out) throws IOException, RowsExceededException,WriteException{
		if(pdtList != null && pdtList.size()>0 && out != null){
			WritableWorkbook excel = Workbook.createWorkbook(out);
			WritableSheet sheet = excel.createSheet("产品价格清单", 0);
			
			Label productNameHeader = new Label(0,0,"品名");
			sheet.addCell(productNameHeader);
			
//			Label productColorHeader = new Label(1,0,"色号");
//			sheet.addCell(productColorHeader);
			
			Label priceHeader = new Label(1,0, "价格");
			sheet.addCell(priceHeader);
			
			int rowIndex = 1;
			for(Object[] p :pdtList){
				Label pname = new Label(0,rowIndex, p[1].toString());
//				Label pcolor = new Label(1, rowIndex, p.getColorCode());
				Label pprice = new Label(1,rowIndex, p[2].toString());
				
				sheet.addCell(pname);
//				sheet.addCell(pcolor);
				sheet.addCell(pprice);
				
				rowIndex ++;
			}
			
			excel.write();
			excel.close();
		}
	}

	/**
	 * 生成客户应收款清单数据输出
	 * 
	 * @param list
	 * @param out
	 */
	private void genCustomerDueFromListExcel(List<Customer> list,
			OutputStream out) throws IOException, RowsExceededException,
			WriteException {
		if (list != null && list.size() > 0 && out != null) {
			WritableWorkbook excel = Workbook.createWorkbook(out);
			WritableSheet sheet = excel.createSheet("应收款", 0);

			sheet.mergeCells(0, 0, 2, 0);

			WritableFont boldFont = new WritableFont(WritableFont
					.createFont("黑体"), 18, WritableFont.BOLD);
			WritableCellFormat boldFontFormat = new WritableCellFormat(boldFont);

			Label title = new Label(0, 0, "客户应收款清单", boldFontFormat);
			sheet.addCell(title);

			WritableFont headerFont = new WritableFont(WritableFont.createFont("黑体"),14,WritableFont.BOLD);
			WritableCellFormat headerCellFormat = new WritableCellFormat(headerFont);

			Label columnHeader1 = new Label(0, 1, "客户名称",headerCellFormat);
			Label columnHeader2 = new Label(1, 1, "销售员",headerCellFormat);
			Label columnHeader3 = new Label(2, 1, "应收金额",headerCellFormat);
			sheet.addCell(columnHeader1);
			sheet.addCell(columnHeader2);
			sheet.addCell(columnHeader3);

			NumberFormat twoDps = new NumberFormat("0.000");
			WritableCellFormat twoDpsFormat = new WritableCellFormat(twoDps);

			int rowIndex = 2;
			double totalAmount = 0;
			for (Customer cust : list) {
				if (cust != null) {
					Label nameLabel = new Label(0, rowIndex, cust.getName());
					Label salesLabel = null;
					if (cust.getStaff() != null) {
						salesLabel = new Label(1, rowIndex, cust.getStaff()
								.getName());
					} else {
						salesLabel = new Label(1, rowIndex, "");
					}
					Number amount = new Number(2, rowIndex, cust
							.getAccountSummary(), twoDpsFormat);
					sheet.addCell(nameLabel);
					sheet.addCell(salesLabel);
					sheet.addCell(amount);
					rowIndex++;
					totalAmount = totalAmount
							+ cust.getAccountSummary().doubleValue();
				}
			}
			sheet.mergeCells(0, rowIndex + 2, 1, rowIndex + 2);
			Label totalLabel = new Label(0, rowIndex + 2, "总计金额：",
					boldFontFormat);
			Number totalAmountCell = new Number(2, rowIndex + 2, totalAmount,
					twoDpsFormat);
			sheet.addCell(totalLabel);
			sheet.addCell(totalAmountCell);

			excel.write();
			excel.close();
		}
	}

	/**
	 * 输出客户收支记录清单Excel
	 * 
	 * @param list
	 * @param out
	 * @param isChecingList 是否对账清单
	 */
	private void genCustomerAccountListExcel(List<CustomerAccountListing> list,
			OutputStream out, boolean isChecingList) throws IOException, RowsExceededException,
			WriteException {
		if (list != null && list.size() > 0 && out != null) {
			WritableWorkbook excel = Workbook.createWorkbook(out);
			
			WritableSheet sheet = null;
			if(!isChecingList){
				sheet = excel.createSheet("客户收支记录清单", 0);
			}else{
				sheet = excel.createSheet("客户财务对账清单", 0);
			}
				
			sheet.mergeCells(0, 0, 4, 0);

			NumberFormat amountFormat = new NumberFormat("0.000");
			WritableCellFormat twoDpsFormat = new WritableCellFormat(amountFormat);
			
			WritableFont boldFont = new WritableFont(WritableFont
					.createFont("黑体"), 18, WritableFont.BOLD);
			WritableCellFormat boldFontFormat = new WritableCellFormat(boldFont);

			Label title = null;
			Label customerAmountSumm = null; //客户应收款余额
			int rowIndex = 3; //用于控制循环列表的行数
			
			if(!isChecingList){
				title = new Label(0, 0, "客户收支记录清单" + "  [" + year + "年"
						+ month + "月]", boldFontFormat);
			}else{
				boldFontFormat.setAlignment(Alignment.CENTRE);
				boldFontFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
//				title = new Label(0,0,"浙江万姿布业有限公司客户往来对账单", boldFontFormat);
				title = new Label(0,0,"绍兴好利时布业有限公司客户往来对账单", boldFontFormat);
				if(list != null && list.size()>0){
					sheet.mergeCells(0, 1, 4, 1);
					Customer cust = customerService.get(customerId);
					Label custDesc = new Label(0, 1, "客户名称：" + cust.getName());
					
					WritableFont customerAmountFont = new WritableFont(WritableFont
							.createFont("宋体"), 12);
					
//					DecimalFormat asFormat = new DecimalFormat("0.000"); //客户余额格式
					WritableCellFormat customerAmountFontFormat = new WritableCellFormat(customerAmountFont);
					customerAmountFontFormat.setAlignment(Alignment.RIGHT);
					
					sheet.mergeCells(2, rowIndex, 3, rowIndex); //合并客户期初欠款
					CustomerAccountListing initialCAL = list.get(0);
					Label initialAmountSumm = new Label(2, rowIndex, "客户前期欠款： " + initialCAL.getLastAmount(), customerAmountFontFormat);
					rowIndex ++;
					//准备前期欠款信息
					
					sheet.mergeCells(2, rowIndex+list.size(), 3, rowIndex+list.size()); //合并客户余额的cell
					customerAmountSumm = new Label(2,rowIndex+list.size(), "客户当前欠款：" + cust.getAccountSummary(), customerAmountFontFormat);
					//准备当期欠款信息
					
					sheet.mergeCells(0, rowIndex + list.size() + 2, 3, rowIndex+list.size()+4); //从列表后第二行开始，合并3行
					WritableCellFormat bottomMemoFormat = new WritableCellFormat();
					bottomMemoFormat.setAlignment(Alignment.CENTRE); //居中
					bottomMemoFormat.setVerticalAlignment(VerticalAlignment.CENTRE);//垂直居中
					bottomMemoFormat.setWrap(true); //自动换行
					Label bottomMemoLabel = new Label(0, rowIndex+list.size()+2, 
							"您好！请核对账目，如准确无误，请签字、盖章后回传至我司，如有疑问烦请来电，" +
							"谢谢！七天内贵司未提出异议，未签字盖章回传我司，视同贵公司默认以上账目准确无误！" +
							"\n 电话：0575-84566029/85681332         传真：0575-84786636 ", bottomMemoFormat);
					
					
					sheet.addCell(custDesc);
					sheet.addCell(initialAmountSumm);
					sheet.addCell(customerAmountSumm);
					sheet.addCell(bottomMemoLabel); //插入页底备注
				}
			}
			sheet.addCell(title);
			
			WritableFont headerFont = new WritableFont(WritableFont.createFont("黑体"),14,WritableFont.BOLD);
			WritableCellFormat headerCellFormat = new WritableCellFormat(headerFont);

			Label columnHeader1 = new Label(0, 2, "日期",headerCellFormat);
			sheet.addCell(columnHeader1);
			
			Label columnHeader2 = null;
			Label columnHeader3 = null;
			Label columnHeader4 = null;
			Label columnHeader5 = null;
			
			CellView autoWidthCellView = new CellView();
			autoWidthCellView.setAutosize(true);
			//设置一个自适应宽度的cell
//			sheet.setColumnView(0, autoWidthCellView); //设定记录时间列为自适应宽度
			
			
			if(!isChecingList){
				//收支清单
				columnHeader2 = new Label(1, 2, "客户名称",headerCellFormat);
				columnHeader3 = new Label(2, 2, "财务科目",headerCellFormat);
				columnHeader4 = new Label(3, 2, "明细栏",headerCellFormat);
				columnHeader5 = new Label(4, 2, "金额",headerCellFormat);
				
				
				sheet.addCell(columnHeader2);
				sheet.addCell(columnHeader3);
				sheet.addCell(columnHeader4);
				sheet.addCell(columnHeader5);
				
				sheet.setColumnView(2, autoWidthCellView); //设定科目列为自适应
				sheet.setColumnView(3, autoWidthCellView); //设定memo列为自适应宽度
				
			}else{
				//对账清单
				columnHeader3 = new Label(1, 2, "财务科目",headerCellFormat);
				columnHeader4 = new Label(2, 2, "明细栏",headerCellFormat);
				columnHeader5 = new Label(3, 2, "金额",headerCellFormat);
				
				sheet.addCell(columnHeader3);
				sheet.addCell(columnHeader4);
				sheet.addCell(columnHeader5);
				sheet.setColumnView(1, autoWidthCellView); //设定科目列为自适应
				sheet.setColumnView(2, autoWidthCellView); //设定memo列为53.57宽度，380像素
			}

			DateFormat datef = new DateFormat("yyyy-MM-dd");
			WritableCellFormat datFormat = new WritableCellFormat(datef);
			
			WritableFont memoFont = new WritableFont(WritableFont.createFont("宋体"));
			WritableCellFormat memoFontFormat = new WritableCellFormat(memoFont);
			memoFontFormat.setWrap(true);
			
			for (CustomerAccountListing cal : list) {
				if (cal != null) {
					DateTime dt = new DateTime(0, rowIndex,
							cal.getCreateTime(), datFormat);
					
					Map<String, Object> convertedMap = convertAltEnterKeyCode(cal.getMemo());
					if(!isChecingList){
						Label customer = new Label(1, rowIndex, cal.getCustomerName());
						Label item = new Label(2, rowIndex, cal.getItemName());
						Label memo = null;
						if(convertedMap != null){
							memo = new Label(3, rowIndex, (String)convertedMap.get("memo"), memoFontFormat);
						}else{
							memo = new Label(3, rowIndex, cal.getMemo(), memoFontFormat);
						}
						Number amount = new Number(4, rowIndex, cal.getAmount(),twoDpsFormat);

						sheet.addCell(dt);
						sheet.addCell(customer);
						sheet.addCell(item);
						sheet.addCell(memo);
						sheet.addCell(amount);
					}else{
						Label item = new Label(1, rowIndex, cal.getItemName());
						Label memo = null;
//						if(convertedMap != null){
//							memo = new Label(2, rowIndex, (String)convertedMap.get("memo"), memoFontFormat);
//						}else{
//						} -- commented by Harrison for keep the same style with finance checking print file on 10/16/2010
						//String[] escapeCharacters = {"\n","\r"};				
						//removeEscapeCharacter(cal.getMemo(),escapeCharacters);
						memo = new Label(2, rowIndex, cal.getMemo(), memoFontFormat); //remove the escape characters, for keep the same style with printed content
						Number amount = new Number(3, rowIndex, cal.getAmount(),twoDpsFormat);

						sheet.addCell(dt);
						sheet.addCell(item);
						sheet.addCell(memo);
						sheet.addCell(amount);
					}
					if(convertedMap != null)
						sheet.setRowView(rowIndex, ((Integer)convertedMap.get("lineCount"))*12*20); //一行字符在excel中占12个高度，该方法的height为1/20个像素
					rowIndex++;
				}
			}
			
			excel.write();
			excel.close();
		}
	}
	
	/**
	 * 账龄报表数据输出
	 * 
	 * @param list
	 * @param outStream
	 */
	private void excelAccountAgeReport(List<AccountAgeReport> list,
			OutputStream out) throws IOException, RowsExceededException,
			WriteException {
		if (list != null && list.size() > 0 && out != null) {
			WritableWorkbook excel = Workbook.createWorkbook(out);
			WritableSheet sheet = excel.createSheet("应收款账龄分析", 0);

			sheet.mergeCells(0, 0, 4, 0);

			WritableFont boldFont = new WritableFont(WritableFont
					.createFont("黑体"), 18, WritableFont.BOLD);
			WritableCellFormat boldFontFormat = new WritableCellFormat(boldFont);

			Label title = new Label(0, 0, "客户应收帐款账龄分析报表", boldFontFormat);
			sheet.addCell(title);
			
			WritableFont headerFont = new WritableFont(WritableFont.createFont("黑体"),14,WritableFont.BOLD);
			WritableCellFormat headerCellFormat = new WritableCellFormat(headerFont);
			

			Label columnHeader1 = new Label(0, 2, "客户名称", headerCellFormat);
			Label columnHeader2 = new Label(1, 2, "销售员", headerCellFormat);
			Label columnHeader3 = new Label(2, 2, "总金额", headerCellFormat);
			Label columnHeader4 = new Label(3, 2, "<30天", headerCellFormat);
			Label columnHeader5 = new Label(4, 2, "<60天", headerCellFormat);
			Label columnHeader6 = new Label(5, 2, "<90天", headerCellFormat);
			Label columnHeader7 = new Label(6, 2, ">91天", headerCellFormat);
			Label columnHeader8 = new Label(7, 2, "对账信息", headerCellFormat);

			sheet.addCell(columnHeader1);
			sheet.addCell(columnHeader2);
			sheet.addCell(columnHeader3);
			sheet.addCell(columnHeader4);
			sheet.addCell(columnHeader5);
			sheet.addCell(columnHeader6);
			sheet.addCell(columnHeader7);
			sheet.addCell(columnHeader8);

			NumberFormat twoDps = new NumberFormat("#.##");
			WritableCellFormat twoDpsFormat = new WritableCellFormat(twoDps);

			int rowIndex = 3;
			for (AccountAgeReport dto : list) {
				if (dto != null) {
					Label customer = new Label(0, rowIndex, dto
							.getCustName());
					Label staff = new Label(1, rowIndex, dto.getStaffName());
					Number amount = new Number(2, rowIndex, dto
							.getCustAmountSum(), twoDpsFormat);
					Number amount30 = new Number(3, rowIndex, dto.getAmount30(),
							twoDpsFormat);
					Number amount60 = new Number(4, rowIndex, dto.getAmount60(),
							twoDpsFormat);
					Number amount90 = new Number(5, rowIndex, dto.getAmount90(),
							twoDpsFormat);
					Number amount91 = new Number(6, rowIndex, dto.getAmount90Plus(),
							twoDpsFormat);
					Label memo = new Label(7, rowIndex, dto.getMemo());

					sheet.addCell(customer);
					sheet.addCell(staff);
					sheet.addCell(amount);
					sheet.addCell(amount30);
					sheet.addCell(amount60);
					sheet.addCell(amount90);
					sheet.addCell(amount91);
					sheet.addCell(memo);

					rowIndex++;
				}
			}

			excel.write();
			excel.close();
		}
	}
	
	/**
	 * 根据pattern 解析日期
	 * @param date
	 * @param pattern
	 * @return
	 * @throws ParseException 
	 */
	private Date parseDate(String date , String[] pattern) throws ParseException{
		String[] defaultPattern =  {"yyyy-MM-dd"};
		if(StringUtils.isNotBlank(date)){
			if(pattern != null){
				return DateUtils.parseDate(date, pattern);
			}else{
				return DateUtils.parseDate(date, defaultPattern);
			}
		}
		return null;
	}
	
	
	/**
	 * 替换原始文本中的回车换行符为excel能够识别的强制换行符
	 * @param orginalStr 原始字符
	 * @return <code>Map<String,Object>[{"memo","\012[销]" 或 "\012[销退]"},{"lineCount",number}] </code>
	 * @return
	 */
	private Map<String, Object> convertAltEnterKeyCode(String originalStr){
		if(StringUtils.isNotBlank(originalStr)){
			String intervalStr = null;
			List<String> strList = null;
			
			boolean isNeedProcess = true;
			if(originalStr.indexOf("[销退]") > 0){
				//销售数据
				intervalStr = "[销退]";
			}else if(originalStr.indexOf("[销]") > 0){
				//销退数据
				intervalStr = "[销]";
			}else{
				isNeedProcess = false;
			}
			
			if(isNeedProcess){
				strList = splitString(originalStr,intervalStr);
				StringBuffer sbf = new StringBuffer();
				for(int i=0;i<strList.size();i++){
					String tmpStr = strList.get(i);
					if(i != 0 && sbf.indexOf("\n") < 0){
						sbf.append("\n"+intervalStr);
					}else{
						sbf.append(intervalStr);
					}
					sbf.append(tmpStr.replaceAll("\r", ""));
				}
				log.debug("excel memo output Str : " + sbf.toString());
				Map<String, Object> rtnMap = new HashMap<String, Object>();
				rtnMap.put("memo", sbf.toString());
				rtnMap.put("lineCount",strList.size());
				return rtnMap;
			}
		}
		return null;
	}

	/**
	 * 根据拆分的字符进行字符串拆分
	 * @param oriStr
	 * @param intervalStr
	 * @return
	 */
	private List<String> splitString(String oriStr, String intervalStr){
		List<String> strList = new ArrayList<String>();
		if(StringUtils.isNotBlank(oriStr)){
			boolean hasIntervalStr = true;
			while(hasIntervalStr){
				int index = oriStr.indexOf(intervalStr);
				if(index > 0){
					String preString = oriStr.substring(0, index);
					strList.add(preString);
					oriStr = oriStr.substring(index+(intervalStr.length() + 1));
				}else if(index == 0){
					oriStr = oriStr.substring(index+(intervalStr.length() + 1));
				}else{
					hasIntervalStr = false;
					if(oriStr.length() > 0){
						strList.add(oriStr);
					}
				}
			}
		}
		return strList;
	}

	/**
	 * 去除转义字符
	 * @param orgStr
	 * @return
	 */
	private String removeEscapeCharacter(String orgStr, String[] escapeCharacters){
		String retnStr = null;
		for(String specStr : escapeCharacters){
			String tmpStr = removeSpecChar(orgStr, specStr);
			if(tmpStr != null && retnStr == null){
				retnStr = tmpStr;
			}else if(tmpStr != null){
				retnStr = retnStr + tmpStr;
			}
		}
		return retnStr;
	}
	
	private String removeSpecChar(String orgStr, String chars){
		if(StringUtils.isNotBlank(orgStr) && chars != null && chars.length() > 0 && orgStr.indexOf(chars) >= 0){
			String[] strArray = orgStr.split(chars);
			StringBuffer sbf = new StringBuffer();
			for (String tmpStr : strArray) {
				sbf.append(tmpStr);
			}
			return sbf.toString();
		}
		return null;
	}
	
	/**
	 * @return the filename
	 */
	public String getFilename() {
		return filename;
	}

	/**
	 * @param filename
	 *            the filename to set
	 */
	public void setFilename(String filename) {
		this.filename = filename;
	}

	/**
	 * @return the customerService
	 */
	public CustomerService getCustomerService() {
		return customerService;
	}

	/**
	 * @param customerService
	 *            the customerService to set
	 */
	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	/**
	 * @return the customerName
	 */
	public String getCustomerName() {
		return customerName;
	}

	/**
	 * @param customerName
	 *            the customerName to set
	 */
	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

	/**
	 * @return the year
	 */
	public Integer getYear() {
		return year;
	}

	/**
	 * @param year
	 *            the year to set
	 */
	public void setYear(Integer year) {
		this.year = year;
	}

	/**
	 * @return the month
	 */
	public Integer getMonth() {
		return month;
	}

	/**
	 * @param month
	 *            the month to set
	 */
	public void setMonth(Integer month) {
		this.month = month;
	}

	/**
	 * @return the customerAccountListingService
	 */
	public CustomerAccountListingService getCustomerAccountListingService() {
		return customerAccountListingService;
	}

	/**
	 * @param customerAccountListingService
	 *            the customerAccountListingService to set
	 */
	public void setCustomerAccountListingService(
			CustomerAccountListingService customerAccountListingService) {
		this.customerAccountListingService = customerAccountListingService;
	}

	/**
	 * @return the customerId
	 */
	public Long getCustomerId() {
		return customerId;
	}

	/**
	 * @param customerId the customerId to set
	 */
	public void setCustomerId(Long customerId) {
		this.customerId = customerId;
	}

	/**
	 * @return the startDate
	 */
	public String getStartDate() {
		return startDate;
	}

	/**
	 * @param startDate the startDate to set
	 */
	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	/**
	 * @return the endDate
	 */
	public String getEndDate() {
		return endDate;
	}

	/**
	 * @param endDate the endDate to set
	 */
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}


	/**
	 * @return the productName
	 */
	public String getProductName() {
		return productName;
	}


	/**
	 * @param productName the productName to set
	 */
	public void setProductName(String productName) {
		this.productName = productName;
	}


	/**
	 * @return the colorCode
	 */
	public String getColorCode() {
		return colorCode;
	}


	/**
	 * @param colorCode the colorCode to set
	 */
	public void setColorCode(String colorCode) {
		this.colorCode = colorCode;
	}


	/**
	 * @return the productService
	 */
	public ProductService getProductService() {
		return productService;
	}


	/**
	 * @param productService the productService to set
	 */
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}


	/**
	 * @return the accountAgeReportService
	 */
	public AccountAgeReportService getAccountAgeReportService() {
		return accountAgeReportService;
	}


	/**
	 * @param accountAgeReportService the accountAgeReportService to set
	 */
	public void setAccountAgeReportService(
			AccountAgeReportService accountAgeReportService) {
		this.accountAgeReportService = accountAgeReportService;
	}

}
